//
//  BaseViewController.swift
//  微博Swift
//
//  Created by apple on 15/9/22.
//  Copyright © 2015年 cc. All rights reserved.
//

import UIKit

class BaseViewController: UITableViewController {
    // 标记用户登录
    var isUserLogin = false
    // 访客视图
    var visitorView: VisitorViewController?
    
    // 创建视图层次结构
    override func loadView() {
        // 根据是否登录来切换视图
        isUserLogin ? super.loadView() : setViewControllers()
    }
    
    private func setViewControllers() {
        // 设置控制器的view
        visitorView = VisitorViewController()
        
        // 设置导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "registBtnClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "loginBtnClick")
        
        // 按钮的监听
        visitorView?.loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        visitorView?.registBtn.addTarget(self, action: "registBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        view = visitorView
    }
    
    @objc private func loginBtnClick() {
        print("loginBtnClick")
        
        let vc = OauthViewController()
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    @objc private func registBtnClick() {
        print("registBtnClick")
        
        NetworkingTools.sharedTools.request(RequestMethod.GET, URLString: "http://www.weather.com.cn/data/sk/101010100.html", parameters: nil) { (result, error) -> () in
            print("result=\(result)")
//            print("error=\(error)")
        }
    }
    
}
